Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
calibration_storage.c
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, 2016, NXP Semiconductors N.V.,
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of NXP Semiconductors N.V. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL NXP SEMICONDUCTORS N.V. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 /*! \file calibration_storage.c
29  \brief Provides functions to store calibration to NVM
30 
31  Users who are not using NXP hardware will need to supply their own drivers
32  in place of those defined here.
33 */
34 #include <stdio.h>
35 #include "sensor_fusion.h"
36 #include "driver_KSDK_NVM.h"
37 #include "calibration_storage.h"
38 
40 {
41 #if F_USING_MAG
42  uint8_t *pSrc, *pDst; // scratch pointers
43  int16_t i; // loop counter
44  uint8_t iNVMBuffer[256]; // NVM write buffer (smallest size writeable to flash)
45  uint32_t itmp32;
46 
47  // copy existing magnetic, gyro and accelerometer calibrations to buffer
48  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
49  pDst = iNVMBuffer;
50  for (i = 0; i < 256; i++)
51  *(pDst++) = *(pSrc++);
52  // default to no magnetic calibration in header
53  iNVMBuffer[MAG_NVM_OFFSET] = iNVMBuffer[MAG_NVM_OFFSET + 1] = iNVMBuffer[MAG_NVM_OFFSET + 2] = iNVMBuffer[MAG_NVM_OFFSET + 3] = 0xFF;
54 
55  // fill the buffer with the magnetic calibration in bytes 0 to 67 (total 68 bytes)
56  // [0-3]: four byte header denoting magnetic calibration present
57  itmp32 = 0x12345678;
58  pSrc = (uint8 *) &itmp32;
59  pDst = iNVMBuffer + MAG_NVM_OFFSET;
60  for (i = 0; i < 4; i++)
61  *(pDst++) = *(pSrc++);
62  // [4-67]: magnetic calibration: 15x float + 1x int32 total 64 bytes
63  pSrc = (uint8 *) &(sfg->MagCal);
64  for (i = 0; i < 64; i++)
65  *(pDst++) = *(pSrc++);
66 
67  // write the whole buffer contents to NVM
68  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
69 #endif // if F_USING_MAG
70  return;
71 }
72 
74 {
75 #if F_USING_GYRO && (F_9DOF_GBY_KALMAN || F_6DOF_GY_KALMAN)
76  uint8_t *pSrc, *pDst; // scratch pointers
77  int16_t i; // loop counter
78  uint8_t iNVMBuffer[256]; // NVM write buffer
79  uint32_t itmp32;
80 
81  // copy existing magnetic, gyro and accelerometer calibrations to buffer
82  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
83  pDst = iNVMBuffer;
84  for (i = 0; i < 256; i++)
85  *(pDst++) = *(pSrc++);
86  // default to no gyroscope calibration in header
87  iNVMBuffer[GYRO_NVM_OFFSET] = iNVMBuffer[GYRO_NVM_OFFSET + 1] = iNVMBuffer[GYRO_NVM_OFFSET + 2] = iNVMBuffer[GYRO_NVM_OFFSET + 3] = 0xFF;
88 
89  // define the four header bytes
90  // [0-3]: four byte header denoting gyro calibration present
91  itmp32 = 0x12345678;
92  pSrc = (uint8 *) &itmp32;
93  pDst = iNVMBuffer + GYRO_NVM_OFFSET;
94  for (i = 0; i < 4; i++)
95  *(pDst++) = *(pSrc++);
96 
97  // [4-15]: 3 gyro offset floats totalling 12 bytes
98 #if F_9DOF_GBY_KALMAN
99  pSrc = (uint8 *) sfg->SV_9DOF_GBY_KALMAN.fbPl;
100 #elif F_6DOF_GY_KALMAN
101  pSrc = (uint8 *) sfg->SV_6DOF_GY_KALMAN.fbPl;
102 #endif
103  for (i = 0; i < 12; i++)
104  *(pDst++) = *(pSrc++);
105 
106  // write the buffer contents to NVM
107  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
108 #endif
109  return;
110 }
111 
113 {
114 #if F_USING_ACCEL
115  uint8_t *pSrc, *pDst; // scratch pointers
116  int16_t i; // loop counter
117  uint8_t iNVMBuffer[256]; // NVM write buffer
118  uint32_t itmp32;
119 
120  // copy existing magnetic, gyro and accelerometer calibrations to buffer
121  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
122  pDst = iNVMBuffer;
123  for (i = 0; i < 256; i++)
124  *(pDst++) = *(pSrc++);
125  // default to no gyroscope calibration in header
126  iNVMBuffer[ACCEL_NVM_OFFSET] = iNVMBuffer[ACCEL_NVM_OFFSET + 1] = iNVMBuffer[ACCEL_NVM_OFFSET + 2] = iNVMBuffer[ACCEL_NVM_OFFSET + 3] = 0xFF;
127 
128  // [0-3]: four byte header denoting accelerometer calibration present
129  itmp32 = 0x12345678;
130  pSrc = (uint8 *) &itmp32;
131  pDst = iNVMBuffer + ACCEL_NVM_OFFSET;
132  for (i = 0; i < 4; i++)
133  *(pDst++) = *(pSrc++);
134 
135  // [4-87]: 21 precision accelerometer calibration floats totalling 84 bytes
136  pSrc = (uint8 *) &(sfg->AccelCal);
137  for (i = 0; i < 84; i++)
138  *(pDst++) = *(pSrc++);
139 
140  // write the buffer contents to NVM
141  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
142 #endif
143  return;
144 }
145 
147 {
148  uint8_t *pSrc, *pDst; // scratch pointers
149  int16_t i; // loop counter
150  uint8_t iNVMBuffer[256]; // NVM write buffer
151 
152  // copy existing magnetic, gyro and accelerometer calibrations to buffer
153  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
154  pDst = iNVMBuffer;
155  for (i = 0; i < 256; i++)
156  *(pDst++) = *(pSrc++);
157 
158  // set no magnetic calibration in header
159  iNVMBuffer[MAG_NVM_OFFSET] = iNVMBuffer[MAG_NVM_OFFSET + 1] = iNVMBuffer[MAG_NVM_OFFSET + 2] = iNVMBuffer[MAG_NVM_OFFSET + 3] = 0xFF;
160 
161  // write the buffer to flash
162  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
163 
164  return;
165 }
166 
168 {
169  uint8_t *pSrc, *pDst; // scratch pointers
170  int16_t i; // loop counter
171  uint8_t iNVMBuffer[256]; // NVM write buffer
172 
173  // copy existing magnetic, gyro and accelerometer calibrations to buffer
174  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
175  pDst = iNVMBuffer;
176  for (i = 0; i < 256; i++)
177  *(pDst++) = *(pSrc++);
178 
179  // set no gyroscope calibration in header
180  iNVMBuffer[GYRO_NVM_OFFSET] = iNVMBuffer[GYRO_NVM_OFFSET + 1] = iNVMBuffer[GYRO_NVM_OFFSET + 2] = iNVMBuffer[GYRO_NVM_OFFSET + 3] = 0xFF;
181 
182  // write the buffer to flash
183  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
184 
185  return;
186 }
187 
189 {
190  uint8_t *pSrc, *pDst; // scratch pointers
191  int16_t i; // loop counter
192  uint8_t iNVMBuffer[256]; // NVM write buffer
193 
194  // copy existing magnetic, gyro and accelerometer calibrations to buffer
195  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
196  pDst = iNVMBuffer;
197  for (i = 0; i < 256; i++)
198  *(pDst++) = *(pSrc++);
199 
200  // set no gyroscope calibration in header
201  iNVMBuffer[ACCEL_NVM_OFFSET] = iNVMBuffer[ACCEL_NVM_OFFSET + 1] = iNVMBuffer[ACCEL_NVM_OFFSET + 2] = iNVMBuffer[ACCEL_NVM_OFFSET + 3] = 0xFF;
202 
203  // write the buffer to flash
204  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
205 
206  return;
207 }
void EraseGyroCalibrationFromNVM(void)
void SaveMagCalibrationToNVM(SensorFusionGlobals *sfg)
void SaveAccelCalibrationToNVM(SensorFusionGlobals *sfg)
void EraseMagCalibrationFromNVM(void)
MagCalibration MagCal
mag cal storage
The top level fusion structure.
uint8_t uint8
Definition: sensor_fusion.h:58
void SaveGyroCalibrationToNVM(SensorFusionGlobals *sfg)
byte NVM_SetBlockFlash(uint8_t *Source, uint32_t Dest, uint16_t Count)
#define F_6DOF_GY_KALMAN
6DOF accel and gyro (Kalman) algorithm selector - 0x2000 to include, 0x0000 otherwise ...
The sensor_fusion.h file implements the top level programming interface.
middleware driver for NVM on Kinetis devices
Provides functions to store calibration to NVM.
void EraseAccelCalibrationFromNVM(void)
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.